import axios from 'axios'

const USE_MOCK = (import.meta.env.VITE_USE_MOCK ?? 'true') !== 'false'
const BASE_API = import.meta.env.VITE_BASE_API || ''

const http = axios.create({
  baseURL: BASE_API,
  timeout: 15000,
  withCredentials: false,
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  }
})

// 请求拦截：自动携带 Token
http.interceptors.request.use((config) => {
  // 从 localStorage 获取 token（也可以从 Pinia store 获取）
  const token = localStorage.getItem('token')
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
})

// 响应拦截：统一处理后端的 {code,message,data,timestamp}
http.interceptors.response.use(
  (response) => {
    // 二进制下载等直接放行
    const rt = response.request?.responseType
    const ct = response.headers?.['content-type'] || ''
    if (rt === 'blob' || rt === 'arraybuffer' || ct.includes('octet-stream')) {
      return response
    }

    const payload = response.data
    // 按后端统一结构解包
    if (payload && typeof payload === 'object' && 'code' in payload && 'data' in payload) {
      if (payload.code === 0) return payload.data
      const err = new Error(payload.message || '业务失败')
      err.code = payload.code
      err.response = response
      throw err
    }
    // 非统一结构（极少数兼容场景）
    return payload
  },
  (error) => {
    if (error.response) {
      const p = error.response.data
      const status = error.response.status
      
      // 处理 401 未授权：清除 Token 并跳转登录页
      if (status === 401) {
        localStorage.removeItem('token')
        localStorage.removeItem('userId')
        localStorage.removeItem('username')
        
        // 跳转到登录页（避免循环重定向）
        if (window.location.pathname !== '/login') {
          window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname)
        }
      }
      
      const msg = p?.message || `HTTP ${status}`
      const err = new Error(msg)
      err.code = p?.code ?? status
      err.response = error.response
      throw err
    } else if (error.request) {
      throw new Error('网络不可用或服务器无响应')
    } else {
      throw new Error(error.message || '请求发生错误')
    }
  }
)

export { http, USE_MOCK }
